<%--
  Created by IntelliJ IDEA.
  User: zyp
  Date: 2021/3/23
  Time: 13:58
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>实现简单的登录功能</title>
</head>
<body>
<%
    //判断是否存在用户名cookie,存在跳转到操作页面
    Cookie cookie = null;
    Cookie[] cookies = null;
    cookies = request.getCookies();
    cookie = getCookieByName(cookies, "userName");
    if (cookie != null) {
        response.sendRedirect("http://localhost:8081/task_5module_1/main.jsp");
    }
%>
<%!
    // 创建方法,用于查找指定名称的cookie

    public static Cookie getCookieByName(Cookie[] cs, String name) {
        if (cs == null || cs.length == 0) {
            return null;
        }
        for (Cookie c : cs) {
            if (name.equals(c.getName())) {
                return c;
            }
        }
        return null;
    }
%>
<form action="loginServlet" method="post">
    用户名:<input type="text" name="userName"/><br/>
    密&nbsp&nbsp&nbsp码:<input type="text" name="password"/><br/>
    <span style="color: red;"><%=request.getAttribute("error") == null ? "" : request.getAttribute("error")%></span><br/>
    <input type="submit" value="登录"/>
</form>
</body>
</html>